const express = require("express")
const pool = require("../utils/DBUtils")
var router = express.Router()

// 查询接口
router.get("/list",async(req,res)=>{
    let rows = await pool.async.query("select * from admin ")
    res.send({
        code:200,
        msg:"查询成功",
        rows:rows
    })
})
// 添加接口
router.post("/add", async (req, res) => {
    let data = req.body
    let id = new Date().getTime()
    let result =await pool.async.query("insert into admin(`id`,`account`,`pwd`,`img`,`login_count`,`last_login_time`,`nickname`) values(?,?,?,?,?,?,?)", [id,data.account,data.pwd,data.img,0,0,data.nickname])
    res.send({
        code: 200,
        msg: "添加成功",
        data: result
    })
})
//删除接口，根据id  删除数据
router.delete("/delete/:id",async(req,res)=>{
    let {id} = req.params
    let result = await pool.async.query("delete from admin where `id`=?",[id])
    res.send({
        code:200,
        msg:"删除成功"
    })
})

//修改接口,根据id修改数据
router.put("/update",async(req,res)=>{
    let data = req.body
    let result = await pool.async.query("UPDATE admin SET pwd=?, nickname=?, img=? WHERE `id`=?",[data.pwd,data.nickname,data.img,data.id])  
    res.send({
        code: 200,
        msg: "修改成功",
        data: result
    })
})
module.exports = router
